package cfengMiddleware.server.controller;

import cfengMiddleware.server.dto.UserOderDto;
import cfengMiddleware.server.service.DeadUserOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Cfeng
 * @date 2022/9/18
 * 用户下单记录处理器
 */

@RestController
@RequestMapping("/order")
@RequiredArgsConstructor
public class UserOrderController {

    private final DeadUserOrderService deadUserOrderService;

    // BindingResult就是验证的结果
    @PostMapping("/purchase")
    public ResponseEntity<String> purchaseGoods(@Validated @RequestBody UserOderDto userOderDto, BindingResult bindingResult) {
        //验证参数的合法性
        if(bindingResult.hasErrors()) {
            //参数不合法
            return new ResponseEntity(HttpStatus.BAD_REQUEST);
        }
        try {
            deadUserOrderService.pushUserOrder(userOderDto);
        } catch (Exception e) {
            return new ResponseEntity(HttpStatus.BAD_REQUEST);
        }
        return ResponseEntity.ok("请求成功");
    }

}
